package com.csu.panel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.RoundRectangle2D;
import java.util.ArrayList;
import java.util.Calendar;

import com.csu.consts.UiConsts;
import com.csu.chart.InfoChartStatics;
import com.csu.ds.IncomeItem;
import com.csu.db.DBUtils;
import com.csu.ds.ExpdItem;
import com.csu.ds.AmountByMonth;
import com.csu.ui.LoginUI;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.XYDataset;

public class InfoPanel_Medium extends JPanel {

    JPanel chartPanel;
    private SubInfoPanel_Medium[] subInfoPanel = new SubInfoPanel_Medium[4];

    public InfoPanel_Medium(){

        setBackground(new Color(238, 246, 254));
        setLayout(null);

        for( int i = 0; i < 4; i ++ ){
            subInfoPanel[i] = new SubInfoPanel_Medium();
            add(subInfoPanel[i]);
        }

        //设置四个子panel的位置
        subInfoPanel[0].setBounds(65, 30, UiConsts.WIDTH_SUB_INFO_PANEL, UiConsts.HEIGHT_SUB_INFO_PANEL);
        subInfoPanel[1].setBounds(425, 30, UiConsts.WIDTH_SUB_INFO_PANEL, UiConsts.HEIGHT_SUB_INFO_PANEL);
        subInfoPanel[2].setBounds(65, 390, UiConsts.WIDTH_SUB_INFO_PANEL, UiConsts.HEIGHT_SUB_INFO_PANEL);
        subInfoPanel[3].setBounds(425, 390, UiConsts.WIDTH_SUB_INFO_PANEL, UiConsts.HEIGHT_SUB_INFO_PANEL);

        //设置四个子panel的标题
        subInfoPanel[0].setTitle("总资产");
        subInfoPanel[1].setTitle("本月支出");
        subInfoPanel[2].setTitle("收入情况");
        subInfoPanel[2].label_title.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                LoginUI.mainUI.showIncome();
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        subInfoPanel[3].setTitle("支出情况");
        subInfoPanel[3].label_title.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                LoginUI.mainUI.showPay();
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

        //获取收入支出表数据
        ArrayList<IncomeItem> incomeItems = DBUtils.getIncomeData();
        ArrayList<ExpdItem> expdItems = DBUtils.getExpenditureData();

        //设置环状图
        JFreeChart ring = InfoChartStatics.generateRingChart( InfoChartStatics.getPieDataset( expdItems ) );
        ChartPanel ringPanel = new ChartPanel(ring);
        ringPanel.setPreferredSize(new Dimension(300,280));

        subInfoPanel[1].add(ringPanel);
        subInfoPanel[1].label_amount.setVisible(false);
        subInfoPanel[1].label_trend.setVisible(false);
        subInfoPanel[1].label_percent.setVisible(false);
        ringPanel.setBounds(25, 45, 300, 280 );

        //设置近六个月收入panel
        ArrayList<AmountByMonth> incomeByMonth = parseIncomeItems(incomeItems);
        double curMonIncome = incomeByMonth.get(5).getAmount();
        double lastMonIncome = incomeByMonth.get(4).getAmount();
        double rate = ( curMonIncome - lastMonIncome ) / lastMonIncome * 100; //计算增长率
        String rateString = String.format("%.2f", rate) + "%";
        subInfoPanel[2].label_amount.setText("¥ "+ String.format("%.2f", curMonIncome ));
        //设置箭头label
        if( rate > 0 ){
            subInfoPanel[2].label_trend.setText("↑");
            subInfoPanel[2].label_percent.setText("+"+rateString);
        }else{
            subInfoPanel[2].label_trend.setText("↓");
            subInfoPanel[2].label_percent.setText(rateString);
        }

        JFreeChart incomeBarChart = InfoChartStatics.generateBarChart( InfoChartStatics.getBarDataset(incomeByMonth), UiConsts.COLOR_LIGHT_BLUE );
        ChartPanel incomeBarChartPanel = new ChartPanel(incomeBarChart);
        incomeBarChartPanel.setPreferredSize(new Dimension(300, 190));
        incomeBarChartPanel.setBounds(15, 115, 300, 190 );
        subInfoPanel[2].add(incomeBarChartPanel);

        //设置近六个月支出panel
        ArrayList<AmountByMonth> expdByMonth = parseExpdItems(expdItems); //整理为月份总数据
        double curMonExpt = expdByMonth.get(5).getAmount();
        double lastMonExpt = expdByMonth.get(4).getAmount();
        //计算比率
        rate = ( curMonExpt - lastMonExpt ) / lastMonExpt * 100;
        rateString = String.format("%.2f", rate) + "%";
        subInfoPanel[3].label_amount.setText("¥ " + String.format("%.2f", curMonExpt ));
        //设置箭头方向和比率
        if( rate > 0 ){
            subInfoPanel[3].label_trend.setText("↑");
            subInfoPanel[3].label_percent.setText("+"+rateString);
        }else{
            subInfoPanel[3].label_trend.setText("↓");
            subInfoPanel[3].label_percent.setText(rateString);
        }
        //生成图标
        JFreeChart epdtBarChart = InfoChartStatics.generateBarChart( InfoChartStatics.getBarDataset(expdByMonth), UiConsts.COLOR_PINK );
        ChartPanel epdtBarChartPanel = new ChartPanel(epdtBarChart);
        epdtBarChartPanel.setPreferredSize(new Dimension(300, 190));
        epdtBarChartPanel.setBounds(15, 115, 300, 190 );
        subInfoPanel[3].add(epdtBarChartPanel);

        //设置总资产panel
        double baseAsset = 300000;
        double asset = baseAsset;
        int month;
        ArrayList<AmountByMonth> assetByMonth = new ArrayList<>();
        for( int i = 0; i < 6; i ++ ){
            month = expdByMonth.get(i).getMonth();
            asset = asset - expdByMonth.get(i).getAmount() + incomeByMonth.get(i).getAmount();
            assetByMonth.add( new AmountByMonth( month, asset ) );
        }

        double curMonAsset = assetByMonth.get(5).getAmount();
        double lastMonAsset = assetByMonth.get(4).getAmount();
        //计算比率
        rate = ( curMonAsset - lastMonAsset ) / lastMonAsset * 100;
        rateString = String.format("%.2f", rate ) + "%";
        subInfoPanel[0].label_amount.setText("¥ " + String.format( "%.2f", curMonAsset ));
        //设置箭头方向和比率
        if( rate > 0 ){
            subInfoPanel[0].label_trend.setText("↑");
            subInfoPanel[0].label_percent.setText("+"+rateString);
        }else{
            subInfoPanel[0].label_trend.setText("↓");
            subInfoPanel[0].label_percent.setText(rateString);
        }
        //生成曲线图
        XYDataset curveDataset = InfoChartStatics.getAssetDataset( assetByMonth );
        JFreeChart curve = InfoChartStatics.generateCurveChart( curveDataset );
        ChartPanel curvePanel = new ChartPanel(curve);
        curvePanel.setPreferredSize(new Dimension(300,190));

        subInfoPanel[0].add(curvePanel);
        curvePanel.setBounds(15, 115, 300, 190);

    }


    public static ArrayList<AmountByMonth> parseIncomeItems(ArrayList<IncomeItem> incomeItems ){ //将收入记录转换为最近六个月的月总记录
        ArrayList<AmountByMonth> amountByMonthList = new ArrayList<>();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new java.util.Date(System.currentTimeMillis()));
        int currentMonth = calendar.get(Calendar.MONTH); //currentMonth为当前月份-1(Month从0开始)

//        int startMonth = currentMonth - 6 + 1;//获取当前月所属前半年的第一个月份
//        IncomeItem curIncomeItem;
//        for( int i = startMonth; i <= currentMonth; i ++ ){
//            AmountByMonth amountByMonth = new AmountByMonth(i,0);
//            for( int x = 0; x < incomeItems.size(); x ++ ){
//                curIncomeItem = incomeItems.get(x);
//                if(curIncomeItem.getDate().getMonth() == i){
//                    amountByMonth.setAmount( amountByMonth.getAmount() + curIncomeItem.getAmount() );
//                }
//            }
//            amountByMonthList.add(amountByMonth);
//        }



        int startMonth = currentMonth - 6 + 1;//获取当前月所属前半年的第一个月份
        IncomeItem curIncomeItem;
        if(startMonth < 0){
            startMonth += 12;
            for( int i = startMonth; i <= 11; i ++ ){
                AmountByMonth amountByMonth = new AmountByMonth(i,0);
                for( int x = 0; x < incomeItems.size(); x ++ ){
                    curIncomeItem = incomeItems.get(x);
                    if(curIncomeItem.getDate().getMonth() == i){
                        amountByMonth.setAmount( amountByMonth.getAmount() + curIncomeItem.getAmount() );
                    }
                }
                amountByMonthList.add(amountByMonth);
            }
            for( int i = 0; i <= currentMonth; i ++ ){
                AmountByMonth amountByMonth = new AmountByMonth(i,0);
                for( int x = 0; x < incomeItems.size(); x ++ ){
                    curIncomeItem = incomeItems.get(x);
                    if(curIncomeItem.getDate().getMonth() == i){
                        amountByMonth.setAmount( amountByMonth.getAmount() + curIncomeItem.getAmount() );
                    }
                }
                amountByMonthList.add(amountByMonth);
            }
        }else{
            for( int i = startMonth; i <= currentMonth; i ++ ){
                AmountByMonth amountByMonth = new AmountByMonth(i,0);
                for( int x = 0; x < incomeItems.size(); x ++ ){
                    curIncomeItem = incomeItems.get(x);
                    if(curIncomeItem.getDate().getMonth() == i){
                        amountByMonth.setAmount( amountByMonth.getAmount() + curIncomeItem.getAmount() );
                    }
                }
                amountByMonthList.add(amountByMonth);
            }
        }


        return amountByMonthList;
    }

    public static ArrayList<AmountByMonth> parseExpdItems(ArrayList<ExpdItem> expdItems ){ //将支出记录转换为最近六个月的月总记录
        ArrayList<AmountByMonth> amountByMonthList = new ArrayList<>();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new java.util.Date(System.currentTimeMillis()));
        int currentMonth = calendar.get(Calendar.MONTH); //currentMonth为当前月份-1(Month从0开始)

//        System.out.println("当前月份" + currentMonth );

        int startMonth = currentMonth - 6 + 1;//获取当前月所属前半年的第一个月份
        ExpdItem curExpdItem;
        if(startMonth < 0){
            startMonth += 12;
            for( int i = startMonth; i <= 11; i ++ ){
                AmountByMonth amountByMonth = new AmountByMonth(i,0);
                for( int x = 0; x < expdItems.size(); x ++ ){
                    curExpdItem = expdItems.get(x);
                    if(curExpdItem.getDate().getMonth() == i){
                        amountByMonth.setAmount( amountByMonth.getAmount() + curExpdItem.getAmount() );
                    }
                }
                amountByMonthList.add(amountByMonth);
            }
            for( int i = 0; i <= currentMonth; i ++ ){
                AmountByMonth amountByMonth = new AmountByMonth(i,0);
                for( int x = 0; x < expdItems.size(); x ++ ){
                    curExpdItem = expdItems.get(x);
                    if(curExpdItem.getDate().getMonth() == i){
                        amountByMonth.setAmount( amountByMonth.getAmount() + curExpdItem.getAmount() );
                    }
                }
                amountByMonthList.add(amountByMonth);
            }
        }else{
            for( int i = startMonth; i <= currentMonth; i ++ ){
                AmountByMonth amountByMonth = new AmountByMonth(i,0);
                for( int x = 0; x < expdItems.size(); x ++ ){
                    curExpdItem = expdItems.get(x);
                    if(curExpdItem.getDate().getMonth() == i){
                        amountByMonth.setAmount( amountByMonth.getAmount() + curExpdItem.getAmount() );
                    }
                }
                amountByMonthList.add(amountByMonth);
            }
        }



        return amountByMonthList;
    }


    @Override
    public void paint(Graphics g){
        int fieldX = 0;
        int fieldY = 0;
        int fieldWidth = getSize().width;
        int fieldHeight = getSize().height;
        RoundRectangle2D rect = new RoundRectangle2D.Double(fieldX,fieldY,fieldWidth,fieldHeight,20,20);
        g.setClip(rect);
        super.paint(g);
    }

}



